//
//  WithdrawPopBankListView.swift
//  d11
//
//  Created by peale on 2018/11/15.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit
typealias BankSelectBlock = (_ index: Int,_ dict:[String:Any]?) -> Void

class WithdrawPopBankListView: UIView,UITableViewDelegate,UITableViewDataSource {
    
    let  reuseIdentifierID : String = "WithdrawPopBankListViewCell"
    var bankSelectBlock: BankSelectBlock?
    func callClickBlock(_ block: @escaping BankSelectBlock) {
        
        bankSelectBlock = block
    }
    var dialogView = UIView()
    
    var backgroundView = UIView()
    var arrayList : [[String:Any]]? //数据列表
    var type : Int = 0
    var selectDict : [String:Any]?
    var top : CGFloat = 0
    lazy var tableView:UITableView = {
        let table = UITableView(frame:CGRect(x: 0, y: 0, width: self.width, height:self.height), style:UITableView.Style.plain)
        table.dataSource = self
        table.delegate = self
        //table.layoutMargins = UIEdgeInsets.zero
        table.separatorStyle = .none
        table.backgroundColor  = UIColor.clear
        table.showsVerticalScrollIndicator = false
        table.tableFooterView = UIView.init()
        table.separatorStyle = .none
        table.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        table.estimatedRowHeight = 0
        table.estimatedSectionHeaderHeight = 0
        table.estimatedSectionFooterHeight = 0
        return table
    }()
    
    convenience init(list:[[String:Any]]?,top:CGFloat) {
        self.init(frame: UIScreen.main.bounds)
        self.y = self.top
        self.backgroundColor = UIColor.clear
        self.height = self.height - top-kNavigationBarHeight
        self.top = top
        self.arrayList  = list
        if self.arrayList == nil {
            self.arrayList = [[String:Any]]()
        }
//        backgroundView.backgroundColor = UIColor.black
//        backgroundView.alpha = 0.6
//        backgroundView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTappedOnBackgroundView)))
//        self.addSubview(backgroundView)
//
//        backgroundView.snp.makeConstraints { (make) in
//            make.width.equalToSuperview()
//            make.height.equalToSuperview()
//            make.top.equalToSuperview()
 //       }
        
        self.addSubview(self.tableView)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func didTappedOnBackgroundView(){
        self.dissmiss()
        
    }
    
    func show() -> Void{
        
       self.y = -self.height
//         let window: UIWindow  = ((UIApplication.shared.delegate?.window)!)!
//         window.addSubview(self)
        UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 10, options: UIView.AnimationOptions(rawValue: 0), animations: {
            self.y = self.top
        }, completion: { (completed) in
            
        })
    }
    
    func dissmiss(){
          self.y = self.top
        self.isHidden = true
        UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 10, options: UIView.AnimationOptions(rawValue: 0), animations: {
            self.y = -self.height
        }, completion: { (completed) in
             self.isHidden = false
            self.removeFromSuperview()
        })
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  self.arrayList!.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 82
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView.init()
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell :WithdrawPopBankListViewCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierID as String) as? WithdrawPopBankListViewCell
        if cell == nil {
            cell =  WithdrawPopBankListViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifierID as String)
        }
        cell?.setdata(dict: self.arrayList?[indexPath.row])
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.didTappedOnBackgroundView()
        if bankSelectBlock != nil {
            bankSelectBlock!(indexPath.row,self.arrayList?[indexPath.row])
        }
    }
}
